package com.sunyzc.ebaotech.soap.simulator;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SOAPClient {
	private static final Logger log = LoggerFactory.getLogger(SOAPClient.class);
	private static final JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();

	public static void main(String[] args) {
		String servicesURL = ISOAPProcessor.SERVER_HOST_LOCALHOST;
		String currentDate = null;
		int totalCount = 1;
		requesAllLSWebService(servicesURL, currentDate, totalCount);
	}

	public static void requesAllLSWebService(String servicesURL, String currentDate, int totalCount) {
		requestLSWebService(servicesURL + "NetSalesExchange" + "?wsdl", "WANGXIAO", currentDate, totalCount);
	}

	public static void requestLSWebService(String wsdlURL, String clientCode, String currentDate, int totalCount) {
		long startTime = System.currentTimeMillis();
		Client client = clientFactory.createClient(wsdlURL);
		HTTPClientPolicy policy = new HTTPClientPolicy();
		policy.setConnectionTimeout(30 * 1000); // 服务端连接的超时时间，单位为毫秒，默认为30000
		policy.setReceiveTimeout(0); // 获取连接后接收数据的超时时间，单位为毫秒，默认为60000
		((HTTPConduit) client.getConduit()).setClient(policy);
		log.info("创建" + client.getClass().getCanonicalName() + "[wsdlURL=" + wsdlURL + "]用时 " + (System.currentTimeMillis() - startTime) + " 毫秒");
		requestLSWebService(client, clientCode, currentDate, totalCount);
		client.destroy();
	}

	public static void requestLSWebService(Client client, String clientCode, String currentDate, int totalCount) {
		if (clientCode != null && !clientCode.trim().equals("")) {
			List<ISOAPProcessor> processorList = new ArrayList<ISOAPProcessor>();
			try {
				for (int index = 1; index <= totalCount; index++) {
					ISOAPProcessor processor = (ISOAPProcessor) Class.forName(SOAPClient.class.getPackage().getName() + "." + clientCode.toLowerCase() + "." + clientCode + "Processor").newInstance();
					log.info(processor.getClass().getSimpleName() + " 第 " + index + "/" + totalCount + " 次开始");
					processor.setCurrentDate(currentDate);
					processorList.add(processor);
					if (clientCode.equals("WANGXIAO")) {
						requestLSWebService(client, processor, clientCode, "New Underwriting");
						requestLSWebService(client, processor, clientCode, "Policy Value Inquiry");
						requestLSWebService(client, processor, clientCode, "Surrender Inquiry");
						requestLSWebService(client, processor, clientCode, "Surrender");
						// 其他交易类型
					} else {
						// 其他交易渠道
					}
					log.info(processor.getClass().getSimpleName() + " 第 " + index + "/" + totalCount + " 次结束");
				}
			} catch (Throwable e) {
				log.error(e.getMessage(), e);
			}
		}
	}

	public static void requestLSWebService(Client client, ISOAPProcessor processor, String clientCode, String transType) throws Exception {
		long startTime = System.currentTimeMillis();
		String requestBodyContentStr = processor.generateRequestBodyBytes(transType, null);
		log.info("交易[" + transType + "]请求报文体: " + requestBodyContentStr);
		String resultStr = processor.invoke(client, transType, requestBodyContentStr);
		log.info("交易[" + transType + "]响应报文体: " + resultStr.toString());
		// log.info(HttpUtil.httpRequest("http://172.25.12.29:7001/ls/services/NetSalesExchange", "GET", requestBodyContentStr, "UTF-8"));
		log.info("交易[" + transType + "]用时 " + (System.currentTimeMillis() - startTime) + " 毫秒");
	}
}
